home *** CD-ROM | disk | FTP | other *** search
/ Risc World 3 / Risc World 3.iso / SOFTWARE / ISSUE6 / PD / PDF / pdf / c++ / ColourSpace < prev    next >
Text File  |  2003-02-14  |  6KB  |  164 lines

  1. //--------------------------------------------------------------------------
  2. //
  3. //   Copyright (c) 2002, Colin Granville
  4. //
  5. //   All rights reserved.
  6. //
  7. //   Redistribution and use in source and binary forms, with or
  8. //   without modification, are permitted provided that the following 
  9. //   conditions are met:
  10. //
  11. //      * Redistributions of source code must retain the above copyright 
  12. //        notice, this list of conditions and the following disclaimer.
  13. //
  14. //      * Redistributions in binary form must reproduce the above 
  15. //        copyright notice, this list of conditions and the following 
  16. //        disclaimer in the documentation and/or other materials 
  17. //        provided with the distribution.
  18. //
  19. //      * The name Colin Granville may not be used to endorse or promote 
  20. //        products derived from this software without specific prior 
  21. //        written permission.
  22. //
  23. //   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
  24. //   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
  25. //   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
  26. //   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
  27. //   COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
  28. //   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  29. //   (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
  30. //   SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  31. //   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
  32. //   STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  33. //   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
  34. //   OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. //--------------------------------------------------------------------------
  37.  
  38. #include "ColourSpace.h"
  39. #include "GfxState.h"
  40.  
  41. inline unsigned int rgb(unsigned int r,unsigned int g,unsigned int b)
  42. {
  43.   return (r<<8) | (g<<16) | (b<<24);
  44. }
  45.  
  46. unsigned int DeviceGreyColourSpace::getRGB(const unsigned char* data)
  47. {
  48.   return rgb(data[0],data[0],data[0]);
  49. }
  50.  
  51. unsigned int CalGreyColourSpace::getRGB(const unsigned char* data)
  52. {
  53.   return rgb(data[0],data[0],data[0]);
  54. }
  55.  
  56. unsigned int DeviceRGBColourSpace::getRGB(const unsigned char* data)
  57. {
  58.   return rgb(data[0],data[1],data[2]);
  59. }
  60.  
  61. unsigned int CalRGBColourSpace::getRGB(const unsigned char* data)
  62. {
  63.   return rgb(data[0],data[1],data[2]);
  64. }
  65.  
  66. unsigned int DeviceCMYKColourSpace::getRGB(const unsigned char* data)
  67. {
  68.   unsigned int r=255-data[0]-data[3]; if (r>255) r=0;
  69.   unsigned int g=255-data[1]-data[3]; if (g>255) g=0;
  70.   unsigned int b=255-data[2]-data[3]; if (b>255) b=0;
  71.   return rgb(r,g,b);
  72. }
  73.  
  74. unsigned int LabColourSpace::getRGB(const unsigned char* data)
  75. {
  76.   //return RGB as proper conversion will be far too slow
  77.   return rgb(data[0],data[1],data[2]);
  78. }
  79.  
  80. unsigned int ICCBasedColourSpace::getRGB(const unsigned char* data)
  81. {
  82.   //return RGB as proper conversion will be far too slow
  83.   return rgb(data[0],data[1],data[2]);
  84. }
  85.  
  86. //***************************************************************************************
  87.  
  88. IndexedColourSpace::IndexedColourSpace(unsigned char* lookuptab,int ncomps,ColourSpace* b)
  89.  : lookup(lookuptab),
  90.    numberOfComponents(ncomps),
  91.    base(b)
  92. {}
  93.  
  94. IndexedColourSpace::~IndexedColourSpace() {delete base;}
  95. #include "iostream.h"
  96. unsigned int IndexedColourSpace::getRGB(const unsigned char* data)
  97. {
  98.  
  99.   return base->getRGB(&lookup[data[0]*numberOfComponents]);
  100. }
  101.  
  102. //***************************************************************************************
  103.  
  104. unsigned int SeparationColourSpace::getRGB(const unsigned char* data)
  105. {
  106.   //return reversed greyscale
  107.   unsigned int n=*data^0xff;
  108.   return rgb(n,n,n);
  109. }
  110.  
  111.  
  112. unsigned int DeviceNColourSpace::getRGB(const unsigned char* data)
  113. {
  114.   //return RGB as proper conversion will be far too slow
  115.   return rgb(data[0],data[1],data[2]);
  116. }
  117.  
  118. unsigned int PatternColourSpace::getRGB(const unsigned char*)
  119. {
  120.   //return RGB as proper conversion will be far too slow
  121.   return 0xffffff00;
  122. }
  123.  
  124. //*****************************************************************
  125.  
  126. ColourSpace* makeColourSpace(GfxColorSpace* cs)
  127. {
  128.   if (!cs) return 0;
  129.   switch (cs->getMode())
  130.   {
  131.     case csDeviceGray: return new DeviceGreyColourSpace;
  132.     case csCalGray:    return new CalGreyColourSpace;
  133.     case csDeviceRGB:  return new DeviceRGBColourSpace;
  134.     case csCalRGB:     return new CalRGBColourSpace;
  135.     case csDeviceCMYK: return new DeviceCMYKColourSpace;
  136.     case csLab:        return new LabColourSpace;
  137.     case csICCBased:   {
  138.                          GfxICCBasedColorSpace* ibcs=(GfxICCBasedColorSpace*)cs;
  139.                          if (!ibcs->getAlt()) return new ICCBasedColourSpace;
  140.                          return makeColourSpace(ibcs->getAlt());
  141.                        }
  142.     case csIndexed:    {
  143.                          GfxIndexedColorSpace* ics=(GfxIndexedColorSpace*)cs;
  144.                          ColourSpace* s=makeColourSpace(ics->getBase());
  145.                          int ncomps=ics->getBase()->getNComps();
  146.                          if (!s) {s= new DeviceRGBColourSpace;ncomps=3;}
  147.                          return new IndexedColourSpace((unsigned char*)ics->getLookup(),ncomps,s);
  148.                        }
  149.     case csSeparation: {
  150.                          GfxSeparationColorSpace* scs=(GfxSeparationColorSpace*)cs;
  151.                          return new SeparationColourSpace;              
  152. //                         if (!scs->getAlt()) return new SeparationColourSpace;
  153. //                         return makeColourSpace(scs->getAlt());
  154.                        }
  155.     case csDeviceN:    {
  156.                          GfxDeviceNColorSpace* dncs=(GfxDeviceNColorSpace*)cs;
  157.                          if (!dncs->getAlt()) return new DeviceNColourSpace;
  158.                          return makeColourSpace(dncs->getAlt());
  159.                        }
  160.     case csPattern:    return new PatternColourSpace;
  161.   }
  162.   return 0;
  163. }
  164.